package com.main.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.dao.VideoDAO;
import com.main.pojo.Video;
import com.main.utils.CommonUtils;
import com.main.utils.Constant;

@Controller
public class VideoController {

	@Autowired
	private VideoDAO videoDao;

	public VideoDAO getVideoDao() {
		return videoDao;
	}

	public void setVideoDao(VideoDAO videoDao) {
		this.videoDao = videoDao;
	}

	@RequestMapping(value = "/video", method = RequestMethod.GET)
	public String homeVideo(Model model, HttpServletRequest request) {
		String path = "";
		try {
			model.addAttribute("videos", videoDao.getVideos(1));
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "videos";
	}

	@RequestMapping(value = "/loadVideo", method = RequestMethod.POST)
	public String loadVideos(@RequestParam(value = "loadedSize", required = false) int loadedSize, Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<Video> videos = new ArrayList<Video>();
			if (loadedSize >= 0) {
				videos = videoDao.getVideos((loadedSize / Constant.VIDEO_PAGE_SIZE) + 1);
			}
			model.addAttribute("videos", videos);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "videosContent";
	}

	@RequestMapping(value = "/video/{videoId}", method = RequestMethod.GET)
	public String getVideo(@PathVariable int videoId, Model model, HttpServletRequest request) {
		String path = "";
		try {
			model.addAttribute("video", videoDao.getVideoById(videoId));
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "viewVideo";
	}

	@RequestMapping(value = "/video-most-view", method = RequestMethod.GET)
	public @ResponseBody
	List<Video> getMostView() {
		List<Video> results = new ArrayList<Video>();
		try {
			results = videoDao.getMostView();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
	
	@RequestMapping(value = "/hot-video", method = RequestMethod.GET)
	public @ResponseBody
	List<Video> getHotVideo() {
		List<Video> results = new ArrayList<Video>();
		try {
			results = videoDao.getHotVideo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
	
	@RequestMapping(value = "/video/update-comment", method = RequestMethod.POST)
	public @ResponseBody boolean updateComment(@RequestParam(value = "id") int id) {
		try {
			videoDao.updateComment(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

}
